package com.sbr.esbmonitor.jmx;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.sbr.esbmonitor.servicemix.Server;

public abstract class JMXRepository {

	protected MBeanServerConnection connect(Server server) {
		JMXServiceURL url = null;
		try {
			url = new JMXServiceURL(server.getJmxUrl());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		JMXConnector jmxc = null;
		HashMap<String,Object> env = new HashMap<String,Object>();
		env.put(JMXConnector.CREDENTIALS,new String[] {server.getUserName(), server.getPassword()});
		try {
			jmxc = JMXConnectorFactory.connect(url, env);
		} catch (IOException e) {
			e.printStackTrace();
		}
		MBeanServerConnection mbsc = null;
		try {
			mbsc = jmxc.getMBeanServerConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return mbsc;
	}
	
	
}
